package com.njtech.smartuniversity.controller;

import com.github.pagehelper.PageInfo;
import com.njtech.smartuniversity.bean.InformBean;
import com.njtech.smartuniversity.service.InformService;
import com.njtech.smartuniversity.util.JsonResult;
import com.njtech.smartuniversity.util.JsonUtil;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * Created by ritchie on 6/25/18
 */

@RestController
@Api(value = "/api/inform", tags = {"通知"}, description = "通知相关")
@RequestMapping("/api/inform")
public class InformController {

    @Autowired
    InformService mInformService;


//    @GetMapping("/{id}")
//    @PreAuthorize("hasRole('ROLE_ADMIN') or hasRole('ROLE_CUSTOMER') or hasRole('ROLE_MERCHANT')")
//    @ApiOperation(value = "单个用户查询接口", notes = "使用ID查询用户")
//    @ApiImplicitParams({
//            @ApiImplicitParam(value = "用户ID(学号/工号)", name = "id", dataType = "String", paramType = "path", required = true)
//    })
//    @ApiResponses({
//            @ApiResponse(code = 400, message = "请求参数有误"),
//            @ApiResponse(code = 401, message = "未授权"),
//            @ApiResponse(code = 403, message = "禁止访问"),
//            @ApiResponse(code = 404, message = "请求路径不存在"),
//            @ApiResponse(code = 500, message = "服务器内部错误")
//    })
//    public JsonResult GetUser(@PathVariable("id") String id) {
//        UserBean user = mUserService.selectByUserId(id);
//        if (user != null) {
//            return new JsonUtil().successDataDefault(user);
//        }
//
//        return new JsonUtil().wrongNoDataWithMessage("不存在该地域");
//    }

    @GetMapping("/list")
    @PreAuthorize("hasRole('ROLE_ADMIN')")
    @ApiOperation(value = "地域列表接口", notes = "获取所有地域信息")
    @ApiResponses({
            @ApiResponse(code = 400, message = "请求参数有误"),
            @ApiResponse(code = 401, message = "未授权"),
            @ApiResponse(code = 403, message = "禁止访问"),
            @ApiResponse(code = 404, message = "请求路径不存在"),
            @ApiResponse(code = 500, message = "服务器内部错误")
    })
    public JsonResult ListAreas() {
        List<InformBean> areaBeanList = mInformService.getAllUsers();
        if (areaBeanList != null) {
            return new JsonUtil().successDataDefault(areaBeanList);
        }
        return new JsonUtil().wrongNoDataWithMessage("不存在地域信息");

    }


    @GetMapping()
    @PreAuthorize("hasRole('ROLE_ADMIN')")
    @ApiOperation(value = "地域列表分页接口", notes = "获取分页地域信息")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "页码", name = "page", dataType = "int", paramType = "form"),
            @ApiImplicitParam(value = "每页数量", name = "size", dataType = "int", paramType = "form")
    })
    @ApiResponses({
            @ApiResponse(code = 400, message = "请求参数有误"),
            @ApiResponse(code = 401, message = "未授权"),
            @ApiResponse(code = 403, message = "禁止访问"),
            @ApiResponse(code = 404, message = "请求路径不存在"),
            @ApiResponse(code = 500, message = "服务器内部错误")
    })
    public JsonResult ListPageUsers(@RequestParam(value = "page", defaultValue = "1") int pageNum,
                                    @RequestParam(value = "size", defaultValue = "10") int pageSize) {
        PageInfo<InformBean> pageInfo = mInformService.getAreasByPage(pageNum, pageSize);
        if (pageInfo != null) {
            return new JsonUtil().successDataDefault(pageInfo);
        }
        return new JsonUtil().wrongNoDataWithMessage("不存在地域信息");

    }

    @DeleteMapping(value = "/{id}")
    @PreAuthorize("hasRole('ROLE_ADMIN')")
    @ApiOperation(value = "地域删除接口", notes = "地域指定ID的用户")
    @ApiImplicitParam(value = "地域ID", name = "id", dataType = "int", paramType = "path", required = true)
    @ApiResponses({
            @ApiResponse(code = 400, message = "请求参数有误"),
            @ApiResponse(code = 401, message = "未授权"),
            @ApiResponse(code = 403, message = "禁止访问"),
            @ApiResponse(code = 404, message = "请求路径不存在"),
            @ApiResponse(code = 500, message = "服务器内部错误")
    })
    public JsonResult DeleteArea(@PathVariable("id") int id) {
        boolean result = mInformService.deleteById(id);
        if (result) {

            return new JsonUtil().successEmptyDataDefault("删除该地域成功");

        }

        return new JsonUtil().wrongNoDataWithMessage("不存在该地域");

    }


//    @PatchMapping(value = "/{id}")
//    @PreAuthorize("hasRole('ROLE_ADMIN')")
//    @ApiOperation(value = "地域信息修改接口", notes = "修改指定ID的地域信息")
//    @ApiImplicitParams({
//            @ApiImplicitParam(value = "地域ID", name = "id", dataType = "int", paramType = "form", required = true),
//            @ApiImplicitParam(value = "地域名", name = "area", dataType = "String", paramType = "form")
//    })
//    @ApiResponses({
//            @ApiResponse(code = 400, message = "请求参数有误"),
//            @ApiResponse(code = 401, message = "未授权"),
//            @ApiResponse(code = 403, message = "禁止访问"),
//            @ApiResponse(code = 404, message = "请求路径不存在"),
//            @ApiResponse(code = 500, message = "服务器内部错误")
//    })
//    public JsonResult UpdateArea(@PathVariable("id") int id,@RequestParam(value = "area", required = false) String area) {
//        InformBean areaBean= new InformBean();
//        areaBean.setArea(area);
//        areaBean.setId(id);
//
//        return new JsonUtil().successDataWithMessage(mAreaService.update(areaBean), "修改用户信息成功");
//    }


    @PostMapping("/add")
    @PreAuthorize("hasRole('ROLE_ADMIN')")
    @ApiOperation(value = "通知通告接口添加", notes = "地域添加")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "标题", name = "title", dataType = "String", paramType = "form"),
            @ApiImplicitParam(value = "级别", name = "level", dataType = "Integer", paramType = "form"),
            @ApiImplicitParam(value = "内容", name = "content", dataType = "String", paramType = "form")
    })
    public JsonResult register(
            @RequestParam(value = "title") String title,
            @RequestParam(value = "level") int level,
            @RequestParam(value = "content") String content)
           {

               InformBean areaBean = new InformBean();

        //生成id
//        String newId = String.valueOf(System.currentTimeMillis()).substring(1);
        areaBean.setTitle(title);
        areaBean.setLevel(level);
        areaBean.setTime(new Date());
        areaBean.setContent(content);

        return new JsonUtil().successDataWithMessage(mInformService.addInform(areaBean),
                       "地域添加成功");
    }


}
